<template>
	<view class="content">
		<view class="header">
			<u-section @click="logoOut" font-size="40" sub-color="#0187F8" color="#39CFBD" v-if="realName" :title="realName" sub-title="退出登录"></u-section>
		</view>
		<view class="btn" @click="toVideo">
		</view>
	</view>
</template>

<script>
	import {
		memberInfo,
		deviceQueryActivationOrder,
		memberUpdate,
		deviceQueryUserServerList,
		deviceCreateRealVideo
	} from "@/request/api.js"
	import permision from "@/js_sdk/wa-permission/permission.js"
	export default {
		data() {
			return {
				realName:uni.getStorageSync('userMsg').realName,
				form: {
					name: '',
					intro: '',
					sex: ''
				},
				isOut: null,
				nowtype: 1,
				curveType: [],
				expirationTime: '',
				VideoConsultationData: {},
				showAdvertisement: false,
				advertisementData: {},
			}
		},
		onLoad() {

		},
		methods: {
			logoOut(){
				uni.showModal({
					title: '提示',
					content: '是否退出登录？',
					success: function (res) {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			authentication() {
				memberUpdate({
					realName: this.form.name,
					idcard: this.form.idcard,
					gender: this.form.gender,
					birthday: this.form.birthday,
				}).then((res) => {
					if (res.reCode == 200) {
						this.$u.toast('认证成功！')
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 1000)
					} else {
						this.$u.toast(res.reMsg)
					}
				})
			},
			determine(url) {
				uni.showLoading({
					title: '正在加载，请稍后...'
				})
				if (this.isOut == 0) {
					console.log('去购买')
					this.$u.toast('请前往厚爱健康app进行购买服务')
					uni.hideLoading()
					return
				} else if (this.isOut == 1 && !this.expirationTime) {
					console.log('去激活')
					uni.hideLoading()
					this.$u.toast('请前往厚爱健康app进行激活服务')
					return
				} else if (this.isOut == 1 && this.expirationTime) {
					if (this.imUtils.formatDateTime(new Date().getTime() + 3 * 60 * 60 * 24 * 1000, 'str') > this
						.expirationTime) {
						this.$u.toast(`问诊服务将于${this.expirationTime}到期`, 3000)
						setTimeout(() => {
							console.log('去问诊')
							this.initPermissions(this.VideoConsultationData)
						}, 3000)
					} else {
						console.log('去问诊')
						this.initPermissions(this.VideoConsultationData)
					}
					return
				}
			},
			// 视频问诊
			getVideoConsultation() {
				deviceQueryUserServerList({
					lang: 'zh',
					userId: uni.getStorageSync('userMsg').id,
				}).then(res => {
					if (res.reCode == 200) {
						this.isOut = res.data.isOut
						this.nowtype = res.data.type
						this.expirationTime = res.data.expirationTime
						this.curveType = res.data.curveType
						if (res.data.curveType && res.data.expirationTime) {
							let curD = new Date(this.expirationTime)
							for (let i = 0; i < res.data.curveType.length; i++) {
								switch (res.data.curveType[i]) {
									case 1:
										this.expirationTime = this.imUtils.formatDateTime(curD.setDate(curD
											.getDate() + 3), 'str');
										break;
									case 6:
										this.expirationTime = this.imUtils.formatDateTime(curD.setMonth(curD
											.getMonth() + 1), 'str');
										break;
									case 7:
										this.expirationTime = this.imUtils.formatDateTime(curD.setMonth(curD
											.getMonth() + 3), 'str');
										break;
									case 8:
										this.expirationTime = this.imUtils.formatDateTime(curD.setMonth(curD
											.getMonth() + 6), 'str');
										break;
									case 5:
										this.expirationTime = this.imUtils.formatDateTime(curD.setFullYear(curD
											.getFullYear() + 1), 'str');
										break;
								}
							}
						}
						this.determine()
					} else {
						this.$u.toast(res.reMsg)
					}
				})
			},
			toVideo() {
				memberInfo().then(res => {
					if (res.reCode == 200) {
						uni.setStorageSync('userMsg', res.data);
						deviceQueryActivationOrder({
							userId: uni.getStorageSync('userMsg').id,
							token: uni.getStorageSync('token'),
						}).then(res => {
							if (res.reCode == 200) {
								this.VideoConsultationData = res.data
							}
						})
						if (uni.getStorageSync('token') == '' || uni.getStorageSync('token') == null) {
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							})
							return
						} else if (!(uni.getStorageSync('token') == '' || uni.getStorageSync('token') == null) && !
							uni.getStorageSync('userMsg').idcard) {
							this.$u.toast('请前往厚爱健康app进行实名认证！')
							return
						} else if (!(uni.getStorageSync('token') == '' || uni.getStorageSync('token') == null) &&
							uni.getStorageSync('userMsg').idcard) {
							this.getVideoConsultation()
						}
					}else{
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						})
					}
				}).catch(fali => {});
			},
			// 初始化
			async initPermissions(item) {
				if (uni.getSystemInfoSync().platform == 'ios') {
					//查看相机权限
					//查看录音权限
					await this.judgeIosPermission(["camera", "record"], item);
				} else if (uni.getSystemInfoSync().platform === 'android') {
					//查看相机权限
					//查看录音权限
					await this.requestAndroidPermission(["android.permission.CAMERA",
						"android.permission.RECORD_AUDIO"], item);
				}
			},
			// ios 权限需要动态获取 需要先跳转到视频问诊页面触发权限，否则权限列表中没有权限设置
			judgeIosPermission(permisionIDArr, item) {
				deviceCreateRealVideo({
					lang: 'zh',
					userId: uni.getStorageSync('userMsg').id,
					orderNo: item.txnNo,
					inquirySource: 0
				}).then(res => {
					if (res.reCode == 200 || res.reCode == 501) {
						uni.hideLoading()
						uni.navigateTo({
							url: `/pages/video/video?data=${res.data}&orderNo=${item.txnNo}`
						})
					} else if (res.reCode == 500) {
						uni.showToast({
							title: res.reMsg,
							icon: 'none'
						})
					}
				})
			},
			// android
			async requestAndroidPermission(permisionIDArr, item) {
				var result1 = await permision.requestAndroidPermission(permisionIDArr[0]);
				var result2 = await permision.requestAndroidPermission(permisionIDArr[1]);
				if (result1 == 1 && result2 == 1) {
					deviceCreateRealVideo({
						lang: uni.getStorageSync("language"),
						userId: uni.getStorageSync('userMsg').id,
						orderNo: item.txnNo,
						inquirySource: 0
					}).then(res => {
						console.log(res)
						if (res.reCode == 200 || res.reCode == 501) {
							uni.hideLoading()
							uni.navigateTo({
								url: `/book/video/video?data=${res.data}&orderNo=${item.txnNo}`
							})
						} else if (res.reCode == 500) {
							uni.showToast({
								title: res.reMsg,
								icon: 'none'
							})
						}
					})
				} else if (result1 == 0 && result2 == 0) {
					this.$u.toast('权限未开启，请在设置中打开相机权限和录音权限')
					setTimeout(() => {
						permision.gotoAppPermissionSetting()
					}, 2000)
				} else if (result1 == 1 && result2 == 0) {
					this.$u.toast('录音权限未开启，请在设置中打开录音权限')
					setTimeout(() => {
						permision.gotoAppPermissionSetting()
					}, 2000)
				} else if (result1 == 0 && result2 == 1) {
					this.$u.toast('相机权限未开启，请在设置中打开相机权限')
					setTimeout(() => {
						permision.gotoAppPermissionSetting()
					}, 2000)
				} else {
					this.$u.toast('权限被永久拒绝，请在设置中打开相机权限和录音权限')
					setTimeout(() => {
						permision.gotoAppPermissionSetting()
					}, 2000)
				}
			},
		}
	}
</script>

<style lang="scss">
	.content {
		background-image: url(../../static/bg.jpg);
		background-size: 100% 100%;
		width: 100vw;
		height: 100vh;
		position: relative;
		.header{
			width: 10vw;
			position: absolute;
			top: vw(20);
			right: vw(20);
			font-size: vw(40);
		}
		.btn {
			width: vw(360);
			height: vw(96);
			background-image: url(../../static/btn.png);
			background-size: 100% 100%;
			position: absolute;
			bottom: 30vh;
			left:8vw;
			z-index: 99;
		}
		.slot-content{
			padding: vw(40);
		}
		/deep/.u-section{
			background-color: #fff !important;
			padding: 20rpx;
			display: flex;
			align-items: center;
			border-radius: 20rpx;
		}
		/deep/.u-section__right-info{
			font-size:30rpx !important;
		}
	}
	
</style>